package com.ck.hxc;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class JsonProcessor {

    // 读取 JSON 文件
    public static JSONObject readJsonFile(String path) throws IOException, JSONException {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        }
        return new JSONObject(content.toString());
    }

    // 处理单个 VAD 的结果
    public static String mergeResultForOneVad(JSONObject resultVad) throws JSONException {
        StringBuilder spkStr = new StringBuilder();
        JSONArray rtArray = resultVad.getJSONObject("st").getJSONArray("rt");

        for (int i = 0; i < rtArray.length(); i++) {
            JSONObject rtDic = rtArray.getJSONObject(i);
            //spkStr.append(3 - resultVad.getJSONObject("st").getInt("rl"));

            JSONArray wsArray = rtDic.getJSONArray("ws");
            for (int j = 0; j < wsArray.length(); j++) {
                JSONObject wsDic = wsArray.getJSONObject(j);
                JSONArray cwArray = wsDic.getJSONArray("cw");
                for (int k = 0; k < cwArray.length(); k++) {
                    JSONObject cwDic = cwArray.getJSONObject(k);
                    String wArray = cwDic.getString("w");
                    //for (int l = 0; l < wArray.length(); l++) {
                        spkStr.append(wArray);
                    //}
                }
            }
            //spkStr.append("\n");
        }

        return spkStr.toString();
    }

    // 将内容写入文件
    public static String contentToFile(List<String> content, String outputFilePath) throws IOException {
            StringBuilder contentStr = new StringBuilder();
            for (String line : content) {
                contentStr.append(line);
            }
            return contentStr.toString();
    }

    public static String a2(String s) {
        try {
            //String pathXunfei = "D:\\java-project-dome\\springboot3-dome\\springboot-dome1\\target\\classes\\output\\test.txt";
            String outputPathXunfei = "xunfei_output.txt";

            // 读取 JSON 文件
            //JSONObject jsXunfei = readJsonFile(pathXunfei);
            JSONObject jsXunfei = new JSONObject(s);
            JSONObject orderResult = new JSONObject(jsXunfei.getJSONObject("content").getString("orderResult"));

            // 处理 lattice 数据
            JSONArray latticeArray = orderResult.getJSONArray("lattice");
            List<String> content = new ArrayList<>();
            for (int i = 0; i < latticeArray.length(); i++) {
                JSONObject latticeItem = latticeArray.getJSONObject(i);
                JSONObject json1Best = new JSONObject(latticeItem.getString("json_1best"));
                content.add(mergeResultForOneVad(json1Best));
            }

            // 将结果写入文件
            String s1 = contentToFile(content, outputPathXunfei);
            System.out.println("处理完成，结果已写入文件：" + outputPathXunfei);
            return s1;
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return "失败";
    }
}
